
/*
 * JDatePicker.java
 *
 * Created on 7 Apr, 2011, 12:27:41 AM
 */
package org.gpl.SwingComponents.JCalendarWidget.beans;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPopupMenu;
import org.gpl.SwingComponents.JCalendarWidget.JCalendarWidget;
import org.gpl.SwingComponents.JCalendarWidget.NumberFormatter;

/**
 *
 * @author AssAssIn
 */
public class JDatePicker extends javax.swing.JPanel implements PropertyChangeListener {

    private JCalendarWidget calendarWidget;
    private JPopupMenu popup;
    private SimpleDateFormat dateFormat;
    private JFormattedTextField dateFormattedTxt;
    private boolean componentInitialized = false;
    private JButton calendarBtn;
    private NumberFormatter numberFormatter;
    private boolean localizeDigits = false;

    @SuppressWarnings("LeakingThisInConstructor")
    public JDatePicker(JCalendarWidget calendarWidget, SimpleDateFormat dateFormat) {
        this.calendarWidget = calendarWidget;
        this.dateFormat = dateFormat;
        if (calendarWidget == null) {
            this.calendarWidget = new JCalendarWidget();
        }
        if (dateFormat == null) {
            this.dateFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
        }
        initComponents();
        componentInitialized = true;
    }

    public JDatePicker(SimpleDateFormat dateFormat)
    {
        this(null,dateFormat);
    }

    /** Creates new form JDatePicker */
    public JDatePicker() {
        this(null, null);
    }

    private void caledarBtnActionPerformed(ActionEvent evt) {
        popup = new JPopupMenu();
        popup.add(calendarWidget);
        popup.setPreferredSize(calendarWidget.getPreferredSize());
        calendarWidget.getSize();
        Rectangle rect = getBounds();
        Point p = new Point(rect.x + rect.width, rect.y + rect.height);
        int y = rect.y + rect.height;
        int x = rect.x + rect.width;
        x = x - calendarWidget.getPreferredSize().width;
        popup.show(this, getWidth() - (int) popup.getPreferredSize().getWidth(), getHeight());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Component Init">
    private void initComponents() {


        dateFormattedTxt = new JFormattedTextField(this.dateFormat);
        calendarBtn = new javax.swing.JButton();
        int height = dateFormattedTxt.getPreferredSize().height;
        setLayout(new java.awt.BorderLayout(0, 0));

        calendarBtn.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                caledarBtnActionPerformed(evt);
            }
        });
        add(calendarBtn, java.awt.BorderLayout.LINE_END);
        this.dateFormat.setLenient(false);
        add(dateFormattedTxt, BorderLayout.CENTER);
        this.calendarWidget.addPropertyChangeListener(this);
        ImageIcon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(JCalendarWidget.class.getResource("resources/calendar.png")));
        calendarBtn.setIcon(icon);
        calendarBtn.setPreferredSize(new Dimension(20, height));
                setPreferredSize(new Dimension(150, height));

        componentInitialized = true;
    }// </editor-fold>

    // Variables declaration - do not modify
    // End of variables declaration
    public void propertyChange(PropertyChangeEvent evt) {
        String formattedDate = dateFormat.format(evt.getNewValue());
        dateFormat.getDateFormatSymbols();
        if (localizeDigits) {
            //dateFormat.
            StringBuilder sb = null;
            numberFormatter = calendarWidget.getNumberFormatter();
            if (numberFormatter != null) {
                sb = new StringBuilder();
                for (char c : formattedDate.toCharArray()) {
                    if (Character.isDigit(c)) {
                        sb.append(numberFormatter.format(Character.getNumericValue(c)));
                    } else {
                        sb.append(c);
                    }
                }
            }
            formattedDate = sb == null ? formattedDate : sb.toString();
            dateFormattedTxt.setText(formattedDate);
        } else {
            dateFormattedTxt.setValue(evt.getNewValue());
        }

        popup.setVisible(false);
    }

    public JCalendarWidget getCalendarWidget() {
        return calendarWidget;
    }

    public void setCalendarWidget(JCalendarWidget calendarWidget) {
        this.calendarWidget = calendarWidget;
        if(this.calendarWidget != null)
        {
            this.calendarWidget.addPropertyChangeListener(this);
        }
    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        if(componentInitialized)
            dateFormattedTxt.setFont(font);
    }

    public boolean getLocalizeDigits() {
        return localizeDigits;
    }

    public void setLocalizeDigits(boolean forceLocalization) {
        this.localizeDigits = forceLocalization;
    }

    public NumberFormatter getNumberFormatter() {
        return numberFormatter;
    }

    public void setNumberFormatter(NumberFormatter numberFormatter) {
        this.numberFormatter = numberFormatter;
    }

    
    public void setSelectedIcon(Icon selectedIcon) {
        calendarBtn.setSelectedIcon(selectedIcon);
    }

    public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) {
        calendarBtn.setRolloverSelectedIcon(rolloverSelectedIcon);
    }

    public void setRolloverIcon(Icon rolloverIcon) {
        calendarBtn.setRolloverIcon(rolloverIcon);
    }

    public void setRolloverEnabled(boolean b) {
        calendarBtn.setRolloverEnabled(b);
    }

    public void setPressedIcon(Icon pressedIcon) {
        calendarBtn.setPressedIcon(pressedIcon);
    }

    public void setMnemonic(char mnemonic) {
        calendarBtn.setMnemonic(mnemonic);
    }

    public void setMnemonic(int mnemonic) {
        calendarBtn.setMnemonic(mnemonic);
    }

    public void setIcon(Icon defaultIcon) {
        calendarBtn.setIcon(defaultIcon);
        calendarBtn.setPreferredSize(new Dimension(defaultIcon.getIconWidth(),defaultIcon.getIconHeight()));
    }

    public void setFocusPainted(boolean b) {
        calendarBtn.setFocusPainted(b);
    }

    @Override
    public void setEnabled(boolean b) {
        calendarBtn.setEnabled(b);
        dateFormattedTxt.setEnabled(b);
    }

    public void setDisabledSelectedIcon(Icon disabledSelectedIcon) {
        calendarBtn.setDisabledSelectedIcon(disabledSelectedIcon);
    }

    public void setDisabledIcon(Icon disabledIcon) {
        calendarBtn.setDisabledIcon(disabledIcon);
    }

    public void setContentAreaFilled(boolean b) {
        calendarBtn.setContentAreaFilled(b);
    }

    public void setBorderPainted(boolean b) {
        calendarBtn.setBorderPainted(b);
    }

    public boolean isRolloverEnabled() {
        return calendarBtn.isRolloverEnabled();
    }

    public boolean isFocusPainted() {
        return calendarBtn.isFocusPainted();
    }

    public boolean isContentAreaFilled() {
        return calendarBtn.isContentAreaFilled();
    }

    public boolean isBorderPainted() {
        return calendarBtn.isBorderPainted();
    }

    public Icon getRolloverSelectedIcon() {
        return calendarBtn.getRolloverSelectedIcon();
    }

    public Icon getRolloverIcon() {
        return calendarBtn.getRolloverIcon();
    }

    public Icon getPressedIcon() {
        return calendarBtn.getPressedIcon();
    }

    public int getMnemonic() {
        return calendarBtn.getMnemonic();
    }

    public Icon getIcon() {
        return calendarBtn.getIcon();
    }

    public void setValue(Object value) {
        dateFormattedTxt.setValue(value);
    }

    public Object getValue() {
        return dateFormattedTxt.getValue();
    }
}
